/**
 *  JSimProd - Java simulation code for production processes
 *  Copyleft Jorge Mira Yagüe <jorge.mira.yague@gmail.com>
 *  Universidad de Alicante 2009
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/*
 * frmStation.java
 *
 * Created on 12-feb-2009, 12:03:22
 */
package gui.forms;

import gui.tools.Receiver;
import gui.tools.models.LMString;
import gui.tools.models.TMInterval;
import gui.tools.models.TMRestriction;
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.JOptionPane;
import javax.swing.event.TableModelEvent;
import lib.exceptions.OverlappedIntervalException;
import lib.model.BufferedStation;
import lib.model.FactoryModel;
import lib.model.Interval;
import lib.model.ItemCategory;
import lib.model.PeriodicInterval;
import lib.model.Restriction;
import lib.model.Station;
import lib.model.Task;
import lib.model.queue.QueueType;

/**
 *
 * @author jorge
 */
public class FrmStation extends javax.swing.JDialog implements Receiver {

    private Station station;
    private FactoryModel factory;
    private Receiver parent;
    private boolean shift;
    private int index;

    /** Creates new form frmStation */
    public FrmStation(Station station, FactoryModel factory, Receiver parent) {
        initComponents();
        this.station = station;
        this.factory = factory;
        this.parent = parent;
        BufferedStation bs = null;

        this.getRootPane().setDefaultButton(btnSave);

        tbRestrictions.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() == 2) {
                    btnEditRestrictionActionPerformed(null);
                }
            }
        });

        String fileString = "/res/icons/logo.png";
        try {
            Image icon = ImageIO.read(getClass().getResource(fileString));
            this.setIconImage(icon);
        } catch (java.io.IOException ioe) {
            System.out.println("Error loading file: " + fileString);
        }

        for (QueueType q : QueueType.values()) {
            cbQueueType.addItem(q.toString());
        }


        if (factory != null) {
            this.setTitle("New station:");
            List<String> stationList = new ArrayList<String>();
            Collection<Station> stations = factory.getStations().values();
            for (Station s : stations) {
                stationList.add(s.getName());
            }
            lstAllStations.setModel(new LMString(stationList));

            List<String> taskList = new ArrayList<String>();
            Collection<ItemCategory> ics = factory.getItemCategories().values();
            for (ItemCategory ic : ics) {
                List<Task> tasks = ic.getTasks();
                for (Task t : tasks) {
                    taskList.add(t.getName());
                }
            }
            lstAllTasks.setModel(new LMString(taskList));
        } else {

            lstAllStations.setModel(new LMString(new ArrayList<String>()));
            lstAllTasks.setModel(new LMString(new ArrayList<String>()));
        }

        lstConnectedStations.setModel(new LMString(new ArrayList<String>()));
        lstExecutedTasks.setModel(new LMString(new ArrayList<String>()));

        if (station != null) {
            txtName.setText(station.getName());
            cbQueueType.setSelectedItem(station.getQueueType());

            List<String> stations = station.getConnections();
            LMString lmAllStations = (LMString) lstAllStations.getModel();
            LMString lmConnectedStations = (LMString) lstConnectedStations.getModel();
            for (String s : stations) {
                lmAllStations.removeString(s);
                lmConnectedStations.addString(s);
            }
            lstAllStations.setModel(lmAllStations);
            lstConnectedStations.setModel(lmConnectedStations);

            List<String> tasks = station.getTasks();
            LMString lmAllTasks = (LMString) lstAllTasks.getModel();
            LMString lmExecutedTasks = (LMString) lstExecutedTasks.getModel();
            for (String t : tasks) {
                lmAllTasks.removeString(t);
                lmExecutedTasks.addString(t);
            }
            lstAllTasks.setModel(lmAllTasks);
            lstExecutedTasks.setModel(lmExecutedTasks);

            if (station instanceof BufferedStation) {
                bs = (BufferedStation) station;
                txtMinBatch.setText(String.valueOf(bs.getMinBatch()));
                txtMaxBatch.setText(String.valueOf(bs.getMaxBatch()));
                tbRestrictions.setModel(new TMRestriction(bs.getRestrictions()));
                chkBuffered.setEnabled(true);
            } else {
                tbRestrictions.setModel(new TMRestriction(new ArrayList<Restriction>()));
                chkBuffered.setEnabled(false);
            }

            try {
                tbRepairs.setModel(new TMInterval(station.getRepairs().getIntervals()));
                tbShifts.setModel(new TMInterval(station.getShifts().getIntervals()));
            } catch (OverlappedIntervalException oie) {
            }
            this.setTitle("Edit station: " + station.getName());
        } else {
            tbRestrictions.setModel(new TMRestriction(new ArrayList<Restriction>()));
            try {
                tbRepairs.setModel(new TMInterval(new ArrayList<Interval>()));
                tbShifts.setModel(new TMInterval(new ArrayList<Interval>()));
            } catch (OverlappedIntervalException oie) {
            }
            this.setTitle("New station:");
        }

        chkBufferedActionPerformed(null);
    }

    public void reciveObject(Object object) {
        try {
            if (object instanceof Restriction) {
                Restriction r = (Restriction) object;
                TMRestriction tmr = (TMRestriction) this.tbRestrictions.getModel();
                if (index == -1) {
                    tmr.addRestriction(r);
                } else {
                    tmr.setRestriction(index, r);
                }
                tbRestrictions.setModel(tmr);
                tbRestrictions.tableChanged(new TableModelEvent(tbRestrictions.getModel()));
            } else if (object instanceof Interval) {
                Interval i = (Interval) object;
                if (shift) {
                    TMInterval tmi = (TMInterval) this.tbShifts.getModel();
                    tmi.addInterval(i);
                    tbShifts.setModel(tmi);
                    tbShifts.tableChanged(new TableModelEvent(tbShifts.getModel()));
                } else {
                    TMInterval tmi = (TMInterval) this.tbRepairs.getModel();
                    tmi.addInterval(i);
                    tbRepairs.setModel(tmi);
                    tbRepairs.tableChanged(new TableModelEvent(tbRepairs.getModel()));
                }
            } else if (object instanceof PeriodicInterval) {
                PeriodicInterval pi = (PeriodicInterval) object;
                if (shift) {
                    TMInterval tmi = (TMInterval) this.tbShifts.getModel();
                    tmi.addIntervals(pi.getAllIntervals());
                    tbShifts.setModel(tmi);
                    tbShifts.tableChanged(new TableModelEvent(tbShifts.getModel()));
                } else {
                    TMInterval tmi = (TMInterval) this.tbRepairs.getModel();
                    tmi.addIntervals(pi.getAllIntervals());
                    tbRepairs.setModel(tmi);
                    tbRepairs.tableChanged(new TableModelEvent(tbRepairs.getModel()));
                }
            }
        } catch (OverlappedIntervalException oie) {
            JOptionPane.showMessageDialog(rootPane,
                    oie.getMessage(),
                    "Error",
                    JOptionPane.ERROR_MESSAGE);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnCancel = new javax.swing.JButton();
        btnSave = new javax.swing.JButton();
        lblName = new javax.swing.JLabel();
        txtName = new javax.swing.JTextField();
        tpStation = new javax.swing.JTabbedPane();
        pnConnections = new javax.swing.JPanel();
        lblAllStations = new javax.swing.JLabel();
        spAllStations = new javax.swing.JScrollPane();
        lstAllStations = new javax.swing.JList();
        btnAddConnections = new javax.swing.JButton();
        btnRemoveConnections = new javax.swing.JButton();
        spConnectedStations = new javax.swing.JScrollPane();
        lstConnectedStations = new javax.swing.JList();
        lblConnectedStations = new javax.swing.JLabel();
        pnTasks = new javax.swing.JPanel();
        lblAllTasks = new javax.swing.JLabel();
        spAllTasks = new javax.swing.JScrollPane();
        lstAllTasks = new javax.swing.JList();
        btnAddTasks = new javax.swing.JButton();
        btnRemoveTasks = new javax.swing.JButton();
        spExecutedTasks = new javax.swing.JScrollPane();
        lstExecutedTasks = new javax.swing.JList();
        lblExecutedTasks = new javax.swing.JLabel();
        pnRestrictions = new javax.swing.JPanel();
        chkBuffered = new javax.swing.JCheckBox();
        txtMaxBatch = new javax.swing.JTextField();
        txtMinBatch = new javax.swing.JTextField();
        lblMinBatch = new javax.swing.JLabel();
        lblMaxBatch = new javax.swing.JLabel();
        lblRestictions = new javax.swing.JLabel();
        btnAddRestriction = new javax.swing.JButton();
        btnEditRestriction = new javax.swing.JButton();
        btnDeleteRestriction = new javax.swing.JButton();
        spRestrictions = new javax.swing.JScrollPane();
        tbRestrictions = new javax.swing.JTable();
        pnShifts = new javax.swing.JPanel();
        lblShifts = new javax.swing.JLabel();
        btnAddShift = new javax.swing.JButton();
        btnDeleteShift = new javax.swing.JButton();
        spShifts = new javax.swing.JScrollPane();
        tbShifts = new javax.swing.JTable();
        pnRepairs = new javax.swing.JPanel();
        spRepairs = new javax.swing.JScrollPane();
        tbRepairs = new javax.swing.JTable();
        lblRepairs = new javax.swing.JLabel();
        btnAddRepair = new javax.swing.JButton();
        btnDeleteRepair = new javax.swing.JButton();
        lblQueueType = new javax.swing.JLabel();
        cbQueueType = new javax.swing.JComboBox();

        setModal(true);
        setResizable(false);

        btnCancel.setText("Cancel");
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });

        btnSave.setText("Save");
        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }
        });

        lblName.setText("Name:");

        lblAllStations.setText("All stations");

        spAllStations.setViewportView(lstAllStations);

        btnAddConnections.setText(">>");
        btnAddConnections.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddConnectionsActionPerformed(evt);
            }
        });

        btnRemoveConnections.setText("<<");
        btnRemoveConnections.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRemoveConnectionsActionPerformed(evt);
            }
        });

        spConnectedStations.setViewportView(lstConnectedStations);

        lblConnectedStations.setText("Connected stations");

        javax.swing.GroupLayout pnConnectionsLayout = new javax.swing.GroupLayout(pnConnections);
        pnConnections.setLayout(pnConnectionsLayout);
        pnConnectionsLayout.setHorizontalGroup(
            pnConnectionsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnConnectionsLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnConnectionsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnConnectionsLayout.createSequentialGroup()
                        .addComponent(spAllStations, javax.swing.GroupLayout.PREFERRED_SIZE, 134, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addGroup(pnConnectionsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(btnRemoveConnections)
                            .addComponent(btnAddConnections))
                        .addGap(22, 22, 22))
                    .addGroup(pnConnectionsLayout.createSequentialGroup()
                        .addComponent(lblAllStations)
                        .addGap(156, 156, 156)))
                .addGroup(pnConnectionsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lblConnectedStations, javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnConnectionsLayout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(spConnectedStations, javax.swing.GroupLayout.DEFAULT_SIZE, 129, Short.MAX_VALUE)))
                .addContainerGap())
        );
        pnConnectionsLayout.setVerticalGroup(
            pnConnectionsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnConnectionsLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnConnectionsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnConnectionsLayout.createSequentialGroup()
                        .addGroup(pnConnectionsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblAllStations)
                            .addComponent(lblConnectedStations))
                        .addGroup(pnConnectionsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(pnConnectionsLayout.createSequentialGroup()
                                .addGap(38, 38, 38)
                                .addComponent(btnAddConnections)
                                .addGap(18, 18, 18)
                                .addComponent(btnRemoveConnections))
                            .addGroup(pnConnectionsLayout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(spConnectedStations, javax.swing.GroupLayout.DEFAULT_SIZE, 251, Short.MAX_VALUE))))
                    .addComponent(spAllStations, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 250, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        tpStation.addTab("Connections", pnConnections);

        lblAllTasks.setText("All tasks");

        spAllTasks.setViewportView(lstAllTasks);

        btnAddTasks.setText(">>");
        btnAddTasks.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddTasksActionPerformed(evt);
            }
        });

        btnRemoveTasks.setText("<<");
        btnRemoveTasks.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRemoveTasksActionPerformed(evt);
            }
        });

        spExecutedTasks.setViewportView(lstExecutedTasks);

        lblExecutedTasks.setText("Executed tasks");

        javax.swing.GroupLayout pnTasksLayout = new javax.swing.GroupLayout(pnTasks);
        pnTasks.setLayout(pnTasksLayout);
        pnTasksLayout.setHorizontalGroup(
            pnTasksLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnTasksLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnTasksLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnTasksLayout.createSequentialGroup()
                        .addComponent(spAllTasks, javax.swing.GroupLayout.PREFERRED_SIZE, 134, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addGroup(pnTasksLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(btnAddTasks)
                            .addComponent(btnRemoveTasks))
                        .addGap(23, 23, 23))
                    .addGroup(pnTasksLayout.createSequentialGroup()
                        .addComponent(lblAllTasks)
                        .addGap(174, 174, 174)))
                .addGroup(pnTasksLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblExecutedTasks)
                    .addComponent(spExecutedTasks, javax.swing.GroupLayout.DEFAULT_SIZE, 128, Short.MAX_VALUE))
                .addContainerGap())
        );
        pnTasksLayout.setVerticalGroup(
            pnTasksLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnTasksLayout.createSequentialGroup()
                .addGroup(pnTasksLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnTasksLayout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(pnTasksLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblAllTasks)
                            .addComponent(lblExecutedTasks))
                        .addGap(13, 13, 13)
                        .addComponent(spExecutedTasks, javax.swing.GroupLayout.PREFERRED_SIZE, 250, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnTasksLayout.createSequentialGroup()
                        .addGap(40, 40, 40)
                        .addComponent(spAllTasks, javax.swing.GroupLayout.DEFAULT_SIZE, 250, Short.MAX_VALUE))
                    .addGroup(pnTasksLayout.createSequentialGroup()
                        .addGap(68, 68, 68)
                        .addComponent(btnAddTasks)
                        .addGap(18, 18, 18)
                        .addComponent(btnRemoveTasks)))
                .addContainerGap())
        );

        tpStation.addTab("Tasks", pnTasks);

        chkBuffered.setText("Is a buffered station");
        chkBuffered.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chkBufferedActionPerformed(evt);
            }
        });

        lblMinBatch.setText("Minimum batch:");

        lblMaxBatch.setText("Maximum batch:");

        lblRestictions.setText("Restrictions:");

        btnAddRestriction.setText("Add...");
        btnAddRestriction.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddRestrictionActionPerformed(evt);
            }
        });

        btnEditRestriction.setText("Edit...");
        btnEditRestriction.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditRestrictionActionPerformed(evt);
            }
        });

        btnDeleteRestriction.setText("Delete");
        btnDeleteRestriction.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteRestrictionActionPerformed(evt);
            }
        });

        tbRestrictions.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        spRestrictions.setViewportView(tbRestrictions);

        javax.swing.GroupLayout pnRestrictionsLayout = new javax.swing.GroupLayout(pnRestrictions);
        pnRestrictions.setLayout(pnRestrictionsLayout);
        pnRestrictionsLayout.setHorizontalGroup(
            pnRestrictionsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnRestrictionsLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnRestrictionsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(chkBuffered)
                    .addGroup(pnRestrictionsLayout.createSequentialGroup()
                        .addGroup(pnRestrictionsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblMinBatch)
                            .addComponent(lblRestictions))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(pnRestrictionsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(pnRestrictionsLayout.createSequentialGroup()
                                .addGap(12, 12, 12)
                                .addComponent(btnAddRestriction)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(btnEditRestriction)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(btnDeleteRestriction))
                            .addGroup(pnRestrictionsLayout.createSequentialGroup()
                                .addComponent(txtMinBatch, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(32, 32, 32)
                                .addComponent(lblMaxBatch)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtMaxBatch, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addComponent(spRestrictions, javax.swing.GroupLayout.DEFAULT_SIZE, 357, Short.MAX_VALUE))
                .addContainerGap())
        );
        pnRestrictionsLayout.setVerticalGroup(
            pnRestrictionsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnRestrictionsLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(chkBuffered)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(pnRestrictionsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblMinBatch)
                    .addComponent(txtMinBatch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblMaxBatch)
                    .addComponent(txtMaxBatch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnRestrictionsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblRestictions)
                    .addComponent(btnAddRestriction)
                    .addComponent(btnEditRestriction)
                    .addComponent(btnDeleteRestriction))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(spRestrictions, javax.swing.GroupLayout.DEFAULT_SIZE, 189, Short.MAX_VALUE)
                .addContainerGap())
        );

        tpStation.addTab("Restrictions", pnRestrictions);

        lblShifts.setText("Shifts:");

        btnAddShift.setText("Add...");
        btnAddShift.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddShiftActionPerformed(evt);
            }
        });

        btnDeleteShift.setText("Delete");
        btnDeleteShift.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteShiftActionPerformed(evt);
            }
        });

        tbShifts.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        spShifts.setViewportView(tbShifts);

        javax.swing.GroupLayout pnShiftsLayout = new javax.swing.GroupLayout(pnShifts);
        pnShifts.setLayout(pnShiftsLayout);
        pnShiftsLayout.setHorizontalGroup(
            pnShiftsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnShiftsLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnShiftsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(spShifts, javax.swing.GroupLayout.DEFAULT_SIZE, 357, Short.MAX_VALUE)
                    .addGroup(pnShiftsLayout.createSequentialGroup()
                        .addComponent(lblShifts)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 164, Short.MAX_VALUE)
                        .addComponent(btnAddShift)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnDeleteShift)))
                .addContainerGap())
        );
        pnShiftsLayout.setVerticalGroup(
            pnShiftsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnShiftsLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnShiftsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblShifts)
                    .addComponent(btnDeleteShift)
                    .addComponent(btnAddShift))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(spShifts, javax.swing.GroupLayout.DEFAULT_SIZE, 241, Short.MAX_VALUE)
                .addContainerGap())
        );

        tpStation.addTab("Shifts", pnShifts);

        tbRepairs.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        spRepairs.setViewportView(tbRepairs);

        lblRepairs.setText("Repairs:");

        btnAddRepair.setText("Add...");
        btnAddRepair.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddRepairActionPerformed(evt);
            }
        });

        btnDeleteRepair.setText("Delete");
        btnDeleteRepair.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteRepairActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pnRepairsLayout = new javax.swing.GroupLayout(pnRepairs);
        pnRepairs.setLayout(pnRepairsLayout);
        pnRepairsLayout.setHorizontalGroup(
            pnRepairsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnRepairsLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnRepairsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(spRepairs, javax.swing.GroupLayout.DEFAULT_SIZE, 357, Short.MAX_VALUE)
                    .addGroup(pnRepairsLayout.createSequentialGroup()
                        .addComponent(lblRepairs)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 154, Short.MAX_VALUE)
                        .addComponent(btnAddRepair)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnDeleteRepair)))
                .addContainerGap())
        );
        pnRepairsLayout.setVerticalGroup(
            pnRepairsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnRepairsLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnRepairsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblRepairs)
                    .addComponent(btnDeleteRepair)
                    .addComponent(btnAddRepair))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(spRepairs, javax.swing.GroupLayout.DEFAULT_SIZE, 241, Short.MAX_VALUE)
                .addContainerGap())
        );

        tpStation.addTab("Repairs", pnRepairs);

        lblQueueType.setText("Queue type:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(tpStation, javax.swing.GroupLayout.DEFAULT_SIZE, 386, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblQueueType)
                            .addComponent(lblName))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(cbQueueType, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE, 211, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(btnSave)
                        .addGap(18, 18, 18)
                        .addComponent(btnCancel)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblName))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblQueueType)
                    .addComponent(cbQueueType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(tpStation, javax.swing.GroupLayout.PREFERRED_SIZE, 329, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCancel)
                    .addComponent(btnSave))
                .addContainerGap())
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-414)/2, (screenSize.height-472)/2, 414, 472);
    }// </editor-fold>//GEN-END:initComponents

    private void chkBufferedActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chkBufferedActionPerformed
        boolean chk = chkBuffered.isSelected();
        lblMinBatch.setEnabled(chk);
        txtMinBatch.setEnabled(chk);
        lblMaxBatch.setEnabled(chk);
        txtMaxBatch.setEnabled(chk);
        lblRestictions.setEnabled(chk);
        btnAddRestriction.setEnabled(chk);
        btnEditRestriction.setEnabled(chk);
        btnDeleteRestriction.setEnabled(chk);
        spRestrictions.setEnabled(chk);
        tbRestrictions.setEnabled(chk);
    }//GEN-LAST:event_chkBufferedActionPerformed

    private void btnAddConnectionsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddConnectionsActionPerformed
        index = lstAllStations.getSelectedIndex();
        if (index >= 0) {
            LMString lsmAll = (LMString) lstAllStations.getModel();
            LMString lsmCon = (LMString) lstConnectedStations.getModel();
            lsmCon.addString(lsmAll.removeString(index));
            lstAllStations.setModel(lsmAll);
            lstConnectedStations.setModel(lsmCon);
        }
    }//GEN-LAST:event_btnAddConnectionsActionPerformed

    private void btnRemoveConnectionsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRemoveConnectionsActionPerformed
        index = lstConnectedStations.getSelectedIndex();
        if (index >= 0) {
            LMString lsmAll = (LMString) lstAllStations.getModel();
            LMString lsmCon = (LMString) lstConnectedStations.getModel();
            lsmAll.addString(lsmCon.removeString(index));
            lstAllStations.setModel(lsmAll);
            lstConnectedStations.setModel(lsmCon);
        }
    }//GEN-LAST:event_btnRemoveConnectionsActionPerformed

    private void btnAddTasksActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddTasksActionPerformed
        index = lstAllTasks.getSelectedIndex();
        if (index >= 0) {
            LMString lsmAll = (LMString) lstAllTasks.getModel();
            LMString lsmExec = (LMString) lstExecutedTasks.getModel();
            lsmExec.addString(lsmAll.removeString(index));
            lstAllTasks.setModel(lsmAll);
            lstExecutedTasks.setModel(lsmExec);
        }
    }//GEN-LAST:event_btnAddTasksActionPerformed

    private void btnRemoveTasksActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRemoveTasksActionPerformed
        index = lstExecutedTasks.getSelectedIndex();
        if (index >= 0) {
            LMString lsmAll = (LMString) lstAllTasks.getModel();
            LMString lsmExec = (LMString) lstExecutedTasks.getModel();
            lsmAll.addString(lsmExec.removeString(index));
            lstAllTasks.setModel(lsmAll);
            lstExecutedTasks.setModel(lsmExec);
        }
    }//GEN-LAST:event_btnRemoveTasksActionPerformed

    private void btnAddRestrictionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddRestrictionActionPerformed
        index = -1;
        new FrmRestriction(null, factory, this).setVisible(true);
    }//GEN-LAST:event_btnAddRestrictionActionPerformed

    private void btnEditRestrictionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditRestrictionActionPerformed
        index = tbRestrictions.getSelectedRow();
        if (index >= 0) {
            Restriction r = ((TMRestriction) tbRestrictions.getModel()).getRestriction(index);
            new FrmRestriction(r, factory, this).setVisible(true);
        }
    }//GEN-LAST:event_btnEditRestrictionActionPerformed

    private void btnDeleteRestrictionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteRestrictionActionPerformed
        index = tbRestrictions.getSelectedRow();
        if (index >= 0) {
            ((TMRestriction) tbRestrictions.getModel()).removeRestriction(index);
        }
    }//GEN-LAST:event_btnDeleteRestrictionActionPerformed

    private void btnAddShiftActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddShiftActionPerformed
        shift = true;
        new FrmInterval(this).setVisible(true);
    }//GEN-LAST:event_btnAddShiftActionPerformed

    private void btnDeleteShiftActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteShiftActionPerformed
        index = tbShifts.getSelectedRow();
        if (index >= 0) {
            ((TMInterval) tbShifts.getModel()).removeInterval(index);
        }
    }//GEN-LAST:event_btnDeleteShiftActionPerformed

    private void btnAddRepairActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddRepairActionPerformed
        shift = false;
        new FrmInterval(this).setVisible(true);
    }//GEN-LAST:event_btnAddRepairActionPerformed

    private void btnDeleteRepairActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteRepairActionPerformed
        index = tbRepairs.getSelectedRow();
        if (index >= 0) {
            ((TMInterval) tbRepairs.getModel()).removeInterval(index);
        }
    }//GEN-LAST:event_btnDeleteRepairActionPerformed

    private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed
        String name = txtName.getText();
        QueueType qt = QueueType.valueOf(cbQueueType.getSelectedItem().toString());
        if (chkBuffered.isSelected()) {
            int min = Integer.parseInt(txtMinBatch.getText());
            int max = Integer.parseInt(txtMaxBatch.getText());
            try {
                station = new BufferedStation(name, qt, min, max);
                TMRestriction tmr = (TMRestriction) tbRestrictions.getModel();
                ((BufferedStation) station).setRestrictions(
                        tmr.getRestrictions());
            } catch (Exception e) {
                JOptionPane.showMessageDialog(rootPane,
                        e.getMessage(),
                        "Error",
                        JOptionPane.ERROR_MESSAGE);
            }
        } else {
            station = new Station(name, qt);
        }

        List<String> stations = ((LMString) lstConnectedStations.getModel()).getStrings();
        for (String s : stations) {
            station.addConnexion(s);
        }

        List<String> tasks = ((LMString) lstExecutedTasks.getModel()).getStrings();
        for (String t : tasks) {
            station.addTask(t);
        }

        try {
            station.addShifts(((TMInterval) tbShifts.getModel()).getIntervals());
            station.addRepairs(((TMInterval) tbRepairs.getModel()).getIntervals());
            parent.reciveObject(station);
            this.setVisible(false);
        } catch (OverlappedIntervalException oie) {
            JOptionPane.showMessageDialog(rootPane,
                    oie.getMessage(),
                    "Error",
                    JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_btnSaveActionPerformed

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
        this.setVisible(false);
    }//GEN-LAST:event_btnCancelActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAddConnections;
    private javax.swing.JButton btnAddRepair;
    private javax.swing.JButton btnAddRestriction;
    private javax.swing.JButton btnAddShift;
    private javax.swing.JButton btnAddTasks;
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnDeleteRepair;
    private javax.swing.JButton btnDeleteRestriction;
    private javax.swing.JButton btnDeleteShift;
    private javax.swing.JButton btnEditRestriction;
    private javax.swing.JButton btnRemoveConnections;
    private javax.swing.JButton btnRemoveTasks;
    private javax.swing.JButton btnSave;
    private javax.swing.JComboBox cbQueueType;
    private javax.swing.JCheckBox chkBuffered;
    private javax.swing.JLabel lblAllStations;
    private javax.swing.JLabel lblAllTasks;
    private javax.swing.JLabel lblConnectedStations;
    private javax.swing.JLabel lblExecutedTasks;
    private javax.swing.JLabel lblMaxBatch;
    private javax.swing.JLabel lblMinBatch;
    private javax.swing.JLabel lblName;
    private javax.swing.JLabel lblQueueType;
    private javax.swing.JLabel lblRepairs;
    private javax.swing.JLabel lblRestictions;
    private javax.swing.JLabel lblShifts;
    private javax.swing.JList lstAllStations;
    private javax.swing.JList lstAllTasks;
    private javax.swing.JList lstConnectedStations;
    private javax.swing.JList lstExecutedTasks;
    private javax.swing.JPanel pnConnections;
    private javax.swing.JPanel pnRepairs;
    private javax.swing.JPanel pnRestrictions;
    private javax.swing.JPanel pnShifts;
    private javax.swing.JPanel pnTasks;
    private javax.swing.JScrollPane spAllStations;
    private javax.swing.JScrollPane spAllTasks;
    private javax.swing.JScrollPane spConnectedStations;
    private javax.swing.JScrollPane spExecutedTasks;
    private javax.swing.JScrollPane spRepairs;
    private javax.swing.JScrollPane spRestrictions;
    private javax.swing.JScrollPane spShifts;
    private javax.swing.JTable tbRepairs;
    private javax.swing.JTable tbRestrictions;
    private javax.swing.JTable tbShifts;
    private javax.swing.JTabbedPane tpStation;
    private javax.swing.JTextField txtMaxBatch;
    private javax.swing.JTextField txtMinBatch;
    private javax.swing.JTextField txtName;
    // End of variables declaration//GEN-END:variables
}
